﻿using CopyMsgToTxt.Helper;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Automation;
using System.Windows.Forms;

public class CopySimulator
{
    // 导入Windows API函数
    [DllImport("user32.dll")]
    private static extern IntPtr WindowFromPoint(Point p);

    // 导入Windows API函数
    [DllImport("user32.dll")]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    // Windows消息常量
    private const uint WM_RBUTTONDOWN = 0x0204;
    private const uint WM_RBUTTONUP = 0x0205;
    private const uint WM_KEYDOWN = 0x0100;
    private const uint WM_KEYUP = 0x0101;
    private const int VK_DOWN = 0x28;
    private const int VK_RETURN = 0x0D;

    // 模拟右键点击并选择菜单项
    public static void SimulateRightClickAndCopy(Point location, IntPtr hWnd)
    {
        //IntPtr targetWnd = WindowFromPoint(location);
        //if (targetWnd != IntPtr.Zero)
        //{
        //    // 发送右键按下和松开的消息
        //    SendMessage(targetWnd, WM_RBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
        //    SendMessage(targetWnd, WM_RBUTTONUP, IntPtr.Zero, IntPtr.Zero);

        //    // 模拟按下向下箭头键和回车键来选择第一个菜单项
        //    SendMessage(targetWnd, WM_KEYDOWN, (IntPtr)VK_DOWN, IntPtr.Zero);
        //    SendMessage(targetWnd, WM_KEYUP, (IntPtr)VK_DOWN, IntPtr.Zero);
        //    SendMessage(targetWnd, WM_KEYDOWN, (IntPtr)VK_RETURN, IntPtr.Zero);
        //    SendMessage(targetWnd, WM_KEYUP, (IntPtr)VK_RETURN, IntPtr.Zero);
        //}
        int x = location.X; // 替换为实际坐标
        int y = location.Y; // 替换为实际坐标
        // 模拟右键按下
        SendMouseInput(x, y, MOUSEEVENTF_RIGHTDOWN);
        // 模拟右键释放
        SendMouseInput(x, y, MOUSEEVENTF_RIGHTUP);

        // 等待菜单弹出
        Thread.Sleep(500);

        // 使用 UI Automation 查找并点击复制菜单项
        AutomationElement rootElement = AutomationElement.RootElement;
        if (rootElement != null)
        {
            // 查找上下文菜单
            AutomationElement contextMenu = rootElement.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Menu));
            if (contextMenu != null)
            {
                // 查找并点击“复制”菜单项
                AutomationElement copyMenuItem = contextMenu.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "复制"));
                if (copyMenuItem != null)
                {
                    InvokePattern invokePattern = copyMenuItem.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                    invokePattern.Invoke();
                    AppLog.WriteInfo("复制操作完成");
                    AppLog.WriteInfo(Clipboard.GetText());
                }
                else
                {
                    AppLog.WriteInfo("复制操作完成");
                }
            }
            else
            {
                AppLog.WriteInfo("未找到上下文菜单");
            }
        }
        else
        {
            AppLog.WriteInfo("未找到根元素");
        }


    }
    [DllImport("user32.dll", SetLastError = true)]
    static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

    [StructLayout(LayoutKind.Sequential)]
    struct INPUT
    {
        public uint type;
        public MOUSEINPUT mi;
    }
    static void SendMouseInput(int x, int y, uint mouseEvent)
    {
        INPUT input = new INPUT
        {
            type = INPUT_MOUSE,
            mi = new MOUSEINPUT
            {
                dx = x * (65536 / GetSystemMetrics(0)), // convert to absolute coordinates
                dy = y * (65536 / GetSystemMetrics(1)), // convert to absolute coordinates
                mouseData = 0,
                dwFlags = mouseEvent | 0x8000, // MOUSEEVENTF_ABSOLUTE
                time = 0,
                dwExtraInfo = IntPtr.Zero
            }
        };

        INPUT[] inputs = new INPUT[] { input };
        SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
    }
    [DllImport("user32.dll")]
    static extern int GetSystemMetrics(int nIndex);
    [StructLayout(LayoutKind.Sequential)]
    struct MOUSEINPUT
    {
        public int dx;
        public int dy;
        public uint mouseData;
        public uint dwFlags;
        public uint time;
        public IntPtr dwExtraInfo;
    }

    const uint INPUT_MOUSE = 0;
    const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
    const uint MOUSEEVENTF_RIGHTUP = 0x0010;


}
